package com.ctshk.rpc.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataBankAccountDTO;
import com.ctshk.rpc.system.entity.MainDataBankAccount;
import com.ctshk.rpc.system.entity.MainDataSubject;
import com.ctshk.rpc.system.mapper.MainDataBankAccountMapper;
import com.ctshk.rpc.system.mapper.MainDataSubjectMapper;
import com.ctshk.rpc.system.req.MainDataBankAccountIdReq;
import com.ctshk.rpc.system.req.MainDataBankAccountListReq;
import com.ctshk.rpc.system.req.MainDataBankAccountReq;
import com.ctshk.rpc.system.service.IMainDataBankAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 銀行賬戶表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-16
 */
@DubboService
public class MainDataBankAccountServiceImpl extends ServiceImpl<MainDataBankAccountMapper, MainDataBankAccount> implements IMainDataBankAccountService {

    @Autowired
    private MainDataSubjectMapper mainDataSubjectMapper;



    @Override
    public Result add(MainDataBankAccountReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        long id = IdWorker.getId();
        MainDataBankAccount bankAccount = EntityUtil.copy(req, MainDataBankAccount.class);
        bankAccount.setId(id);
        if (CollectionUtils.isNotEmpty(req.getDeptType())){
            bankAccount.setDeptType(JSON.toJSONString(req.getDeptType()));
        }
        bankAccount.setCreateId(tokenUser.getId());
        bankAccount.setModifiedId(tokenUser.getId());
        bankAccount.setModifiedName(tokenUser.getUsername());
        bankAccount.setGmtCreate(now);
        bankAccount.setGmtModified(now);
        bankAccount.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(bankAccount);
        return Result.success(id);
    }

    @Override
    public Result update(MainDataBankAccountReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataBankAccount bankAccount = EntityUtil.copy(req, MainDataBankAccount.class);
        if (CollectionUtils.isNotEmpty(req.getDeptType())){
            bankAccount.setDeptType(JSON.toJSONString(req.getDeptType()));
        }
        bankAccount.setModifiedId(tokenUser.getId());
        bankAccount.setModifiedName(tokenUser.getUsername());
        bankAccount.setGmtModified(now);
        baseMapper.updateById(bankAccount);
        return Result.success(req.getId());
    }

    @Override
    public MainDataBankAccountDTO query(MainDataBankAccountIdReq req) {
        MainDataBankAccount mainDataBankAccount = baseMapper.selectById(req.getId());
        MainDataBankAccountDTO mainDataBankAccountDTO = EntityUtil.copy(mainDataBankAccount, MainDataBankAccountDTO.class);
        //本级辅助账类型列表
        if (StringUtils.isNotBlank(mainDataBankAccount.getDeptType())){
            mainDataBankAccountDTO.setDeptType(JSON.parseArray(mainDataBankAccount.getDeptType(),Long.class));
        }
        if (mainDataBankAccount.getSubjectId() != null){
            //查询科目
            MainDataSubject subject = mainDataSubjectMapper.selectById(mainDataBankAccount.getSubjectId());
            if (subject != null && Objects.equals(subject.getIsDeleted(),0) && Objects.equals(subject.getStatus(),0)){
                mainDataBankAccountDTO.setNumber(subject.getNumber());
                mainDataBankAccountDTO.setName(subject.getName());
                mainDataBankAccountDTO.setType(subject.getType());
            }
        }
        return mainDataBankAccountDTO;
    }

    @Override
    public PageResponse<MainDataBankAccountDTO> list(MainDataBankAccountListReq req) {
        Page<MainDataBankAccount> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataBankAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataBankAccount::getStatus,req.getStatus());
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getAccountName()),MainDataBankAccount::getAccountName,req.getAccountName());
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getBankName()),MainDataBankAccount::getBankName,req.getBankName());
        queryWrapper.lambda().orderByDesc(MainDataBankAccount::getGmtModified);
        Page<MainDataBankAccount> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataBankAccount> records = iPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataBankAccountDTO> mainDataBankAccountDTOS = new ArrayList<>();
        for (MainDataBankAccount mainDataBankAccount : records) {
            MainDataBankAccountDTO mainDataBankAccountDTO = EntityUtil.copy(mainDataBankAccount,MainDataBankAccountDTO.class);
            //本级辅助账类型列表
            if (StringUtils.isNotBlank(mainDataBankAccount.getDeptType())){
                mainDataBankAccountDTO.setDeptType(JSON.parseArray(mainDataBankAccount.getDeptType(),Long.class));
            }
            if (mainDataBankAccount.getSubjectId() != null){
                //查询科目
                MainDataSubject subject = mainDataSubjectMapper.selectById(mainDataBankAccount.getSubjectId());
                if (subject != null && Objects.equals(subject.getIsDeleted(),0) && Objects.equals(subject.getStatus(),0)){
                    mainDataBankAccountDTO.setNumber(subject.getNumber());
                    mainDataBankAccountDTO.setName(subject.getName());
                    mainDataBankAccountDTO.setType(subject.getType());
                }
            }
            mainDataBankAccountDTOS.add(mainDataBankAccountDTO);
        }
        return new PageResponse<>(mainDataBankAccountDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }
}
